import { TOKEN_KEY } from '@/common/constants'
import axios from 'axios'

import { createAction } from '@/utils/axios'
import { useInterceptors } from '@/utils/axios/interceptors'

function createInstance() {
  const authStore = localStorage.getItem(TOKEN_KEY)

  return axios.create({
    baseURL: import.meta.env.VITE_BASE_URL,
    headers: {
      'jd-token': `Bearer ${authStore ?? ''}`
    }
  })
}

const axiosInstance = createInstance()

useInterceptors(axiosInstance)

const { getAction, postAction, deleteAction } = createAction(axiosInstance)

export const useGet = getAction

export const usePost = postAction

export const useDelete = deleteAction
